package com.da.web.school_student.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.da.common.Result;
import com.da.web.school_class.entity.SchoolClass;
import com.da.web.school_class.sevice.SchoolClassService;
import com.da.web.school_student.entity.SchoolStudent;
import com.da.web.school_student.entity.StuParm;
import com.da.web.school_student.service.SchoolStudentService;
import com.da.web.sys_role.entity.SysRole;
import com.da.web.sys_role.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author da
 * @time 2023/12/6 下午 3:01
 */
@RestController
@RequestMapping("/api/student")
public class SchoolStudentController {
    @Autowired
    private SchoolStudentService schoolStudentService;
    @Autowired
    private SchoolClassService schoolClassService;
    @Autowired
    private SysRoleService sysRoleService;

    //新增
    @PostMapping
    public Result add(@RequestBody SchoolStudent schoolStudent) {
        //把密码加密
        String password = DigestUtils.md5DigestAsHex(schoolStudent.getPassword().getBytes());
        schoolStudent.setPassword(password);
        schoolStudentService.addStu(schoolStudent);
        return Result.success("新增成功");
    }

    //编辑
    @PutMapping
    public Result edit(@RequestBody SchoolStudent schoolStudent) {
        schoolStudentService.editStu(schoolStudent);
        return Result.success("编辑成功");
    }

    //根据id查询学生信息
    @GetMapping("/getById")
    public Result getById(Long stuId) {
        SchoolStudent byId = schoolStudentService.getStuById(stuId);
        return Result.success("查询成功", byId);
    }

    //通过id删除
    @DeleteMapping("/{stuId}")
    public Result delete(@PathVariable("stuId") Long stuId) {
        schoolStudentService.deleteStu(stuId);
        return Result.success("删除成功");
    }

    //获取分页列表
    @GetMapping("/list")
    public Result getList(StuParm parm) {
        IPage<SchoolStudent> list = schoolStudentService.getList(parm);
        return Result.success("查询成功", list);
    }

    //通过专业id获取对应的所有班级列表
    @GetMapping("/getClassList")
    public Result getClassList(Long majorId) {
        QueryWrapper<SchoolClass> query = new QueryWrapper<>();
        query.lambda().eq(SchoolClass::getMajorId, majorId);
        List<SchoolClass> list = schoolClassService.list(query);
        return Result.success("查询成功", list);
    }

    //查询角色列表
    @GetMapping("/getRoleList")
    public Result getRoleList() {
        List<SysRole> list = sysRoleService.list();
        return Result.success("查询成功", list);
    }

    //通过班级id获取对应班级下面的学生
    @GetMapping("/getClassByClassId")
    public Result getClassByClassId(Long classId) {
        QueryWrapper<SchoolStudent> query = new QueryWrapper<>();
        query.lambda().eq(SchoolStudent::getClassId, classId);
        List<SchoolStudent> list = schoolStudentService.list(query);
        return Result.success("查询成功", list);
    }

    @PostMapping("/resetPassword")
    public Result resetPassword(@RequestBody SchoolStudent schoolStudent) {
        String stuNum = schoolStudent.getStuNum();
        schoolStudent.setPassword(DigestUtils.md5DigestAsHex(stuNum.getBytes()));
        boolean b = schoolStudentService.updateById(schoolStudent);
        if (b) {
            return Result.success("密码重置成功");
        }
        return Result.error("密码重置失败");
    }

}

